﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.DirectoryServices;

namespace ICeFramework.Common.ActiveDirectory
{
    public class RootDirectoryEntry
    {
        private static RootDirectoryEntry instance;
        private static Object lockADConnectionInstance = new Object();
        private ILog logger = LogManager.GetLogger("Common");
        private static string domainName;
        private static string rootPath;
        private static DirectoryEntry ADDirectoryEntry;


        public static void InitDomain(string dm, string rp)
        {
            domainName = dm;
            rootPath = rp;
        }

        public static RootDirectoryEntry Instance
        {
            get
            {
                lock (RootDirectoryEntry.lockADConnectionInstance)
                {
                    if (instance == null)
                        instance = new RootDirectoryEntry();
                    return instance;
                }
            }
        }

        public static void ResetInstance()
        {
            instance = null;
            if (ADDirectoryEntry != null)
            {
                ADDirectoryEntry.Close();
                ADDirectoryEntry.Dispose();
            }
        }

        #region constructor
        private RootDirectoryEntry()
        {
            try
            {
                if (string.IsNullOrEmpty(domainName))
                {
                    ADDirectoryEntry = new DirectoryEntry(this.RootPath);
                }
                else
                {
                    ADDirectoryEntry = this.GetDomainEntry(domainName);
                    if (ADDirectoryEntry == null)
                    {
                        throw new ActiveDirectoryException(string.Format("Domain not found: {0}", domainName));
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Debug(string.Format("Error in method - {0} , message {1}:", "RootDirectoryEntry", ex.Message));
            }
        }
        #endregion

        #region Proprietati
        public ILog Logger
        {
            get { return logger; }
        }
        
        public string RootPath
        {
            get { return rootPath; }
            set { rootPath = value; }
        }

        public DirectoryEntry ADRootDirectoryEntry
        {
            get { return ADDirectoryEntry; }
            set { ADDirectoryEntry = value; }
        }

        


        #endregion Proprietati

        protected DirectoryEntry GetDomainEntry(string domain)
        {
            DirectorySearcher srch = new DirectorySearcher("objectCategory=Domain");
            SearchResultCollection col = srch.FindAll();
            foreach (SearchResult res in srch.FindAll())
            {
                string name = (string)this.GetSinglePropValue(res, "name");
                if (domain == name)
                {
                    return res.GetDirectoryEntry();
                }
            }
            return null;
        }

        protected object GetSinglePropValue(SearchResult result, string propName)
        {
            if (!result.Properties.Contains(propName))
            {
                return null;
            }
            ResultPropertyValueCollection vals = result.Properties[propName];
            if (vals.Count < 0)
            {
                return null;
            }
            return vals[0].ToString();
        }


    }
}
